package com.quad.innovators.salesease.controller.tag;

import com.quad.innovators.salesease.annotation.DataPermission;
import com.quad.innovators.salesease.annotation.FunctionPermission;
import com.quad.innovators.salesease.annotation.Log;
import com.quad.innovators.salesease.annotation.RepeatSubmit;
import com.quad.innovators.salesease.common.result.PageResultBean;
import com.quad.innovators.salesease.common.result.Result;
import com.quad.innovators.salesease.constants.SystemConstants;
import com.quad.innovators.salesease.enums.LogModuleEnum;
import com.quad.innovators.salesease.model.common.Option;
import com.quad.innovators.salesease.model.form.tag.TagForm;
import com.quad.innovators.salesease.model.query.tag.TagQuery;
import com.quad.innovators.salesease.model.vo.tag.TagVO;
import com.quad.innovators.salesease.service.tag.TagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author cheng
 */
@Tag(name = "12.标签管理接口")
@RestController
@RequestMapping(SystemConstants.BACKEND_API + "/tag")
@RequiredArgsConstructor
public class TagController {

    private final TagService tagService;

    @Operation(summary = "分页列表")
    @GetMapping("/page")
    @Log(value = "分页列表", module = LogModuleEnum.TASK_DISTRIBUTION)
    @DataPermission(permissionCode = "base:tag:data-permission")
    public Result<PageResultBean<TagVO>> listPaged(TagQuery queryParams) {
        PageResultBean<TagVO> result = tagService.listPaged(queryParams);
        return Result.success(result);
    }

    @Operation(summary = "根据模块查询标签列表（下拉框）")
    @GetMapping("/options")
    @DataPermission(permissionCode = "base:tag:data-permission")
    public Result<List<Option<String>>> getList() {
        List<Option<String>> result = tagService.getList();
        return Result.success(result);
    }

    @Operation(summary = "新增")
    @PostMapping
    @FunctionPermission(permissionCode = "base:tag:add")
    @RepeatSubmit
    public Result<?> save(@RequestBody @Valid TagForm form) {
        return Result.success(tagService.save(form));
    }

    @Operation(summary = "修改")
    @PutMapping(value = "/{code}")
    @Parameters({
            @Parameter(name = "code", description = "code")
    })
    @FunctionPermission(permissionCode = "base:tag:edit")
    @DataPermission(permissionCode = "base:tag:data-permission")
    public Result<?> update(@PathVariable("code") String code, @RequestBody @Validated TagForm form) {
        return Result.success(tagService.update(code, form));
    }

    @Operation(summary = "修改状态")
    @PutMapping(value = "/{code}/status")
    @Parameters({
            @Parameter(name = "code", description = "code"),
            @Parameter(name = "status", description = "状态(1:启用;0:禁用)")
    })
    @FunctionPermission(permissionCode = "base:tag:status")
    @DataPermission(permissionCode = "base:tag:data-permission")
    public Result<Void> updateStatus(@PathVariable("code") String code, @RequestParam("status") Integer status) {
        boolean result = tagService.updateStatus(code, status);
        return Result.judge(result);
    }

    @Operation(summary = "表单数据")
    @GetMapping("/{code}/form")
    @Parameters({
            @Parameter(name = "code", description = "code"),
    })
    @FunctionPermission(permissionCode = "base:tag:detail")
    @DataPermission(permissionCode = "base:tag:data-permission")
    public Result<TagForm> getForm(@PathVariable("code") String code) {
        TagForm formData = tagService.getFormData(code);
        return Result.success(formData);
    }


}
